package com.reef.impl;

import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.reef.dao.BookcategoryRepository;
import com.reef.dto.BookClassDto;
import com.reef.entity.Book_class;
import com.reef.service.BookcategoryService;
@Service
public class BookcategoryServiceImpl implements BookcategoryService{
@Autowired
  private BookcategoryRepository bookcategoryRepository;
	public List<Book_class> selAll() {
		// TODO Auto-generated method stub
		return bookcategoryRepository.findAll();
	}

	public Book_class add(Book_class book_class) {
		// TODO Auto-generated method stub
		return bookcategoryRepository.save(book_class);
	}

	public Page<Book_class> findByBookclassCondition(BookClassDto bcDto, Pageable pageable) {
		Specification<Book_class> spec = getSpec(bcDto);
		return bookcategoryRepository.findAll(spec, pageable);
	}
	/**
	 * 单独封装的方法
	 * @param tDto
	 * @return
	 */
	public Specification<Book_class> getSpec(final BookClassDto bcDto) {
		return new Specification<Book_class>() {

			public Predicate toPredicate(Root<Book_class> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				//创建SQL表达式
				Predicate p = criteriaBuilder.conjunction();
				//声明表达式的集合
				List<Expression<Boolean>> list = p.getExpressions();
				if (bcDto.getClassName()!=null && bcDto.getClassName()!=" ") {
					list.add(criteriaBuilder.like(root.get("className").as(String.class), "%"+bcDto.getClassName()+"%"));
				}
				
				
				return p;
			}
		};
	}

	public Book_class updateBookclass(Book_class book_class) {
		// TODO Auto-generated method stub
		return bookcategoryRepository.save(book_class);
	}

	public Book_class insertBookclass(Book_class book_class) {
		// TODO Auto-generated method stub
		return bookcategoryRepository.save(book_class);
	}

	public Book_class save(Book_class book_class) {
		// TODO Auto-generated method stub
		return bookcategoryRepository.save(book_class);
	}
}
